package com.ttai.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerHomeFragmentComponet;
import com.ttai.dagger.module.activity.HomeFragmentModule;
import com.ttai.presenter.activity.HomePresrenter;
import com.ttai.untils.Constant;
import com.ttai.untils.WindowUtils;
import com.ttai.untils.getui.DemoIntentService;
import com.ttai.untils.getui.DemoPushService;
import com.umeng.analytics.MobclickAgent;

import javax.inject.Inject;

public class MainActivity extends Activity{

    @Inject
    public HomePresrenter homePresrenter;

    String token = "";
    public static String index = "0";

    private static final int REQUEST_PERMISSION = 0;

    // DemoPushService.class 自定义服务名称, 核心服务
    private Class userPushService = DemoPushService.class;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        index = getIntent().getStringExtra("index");
        System.out.println("index"+index);

//        WindowUtils.showPopupWindow(this);

        //dagger注入
        setComponet();

        PackageManager pkgManager = getPackageManager();

        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        boolean sdCardWritePermission =
                pkgManager.checkPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

        // read phone state用于获取 imei 设备信息
        boolean phoneSatePermission =
                pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

        if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission) {
            requestPermission();
        } else {
            PushManager.getInstance().initialize(this.getApplicationContext(), userPushService);
        }
        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
//        Log.i("123", "onCreate: intentService");
        String cid = PushManager.getInstance().getClientid(this);
//        Log.i("sja", "onCreate: "+cid);

        if (sdCardWritePermission==true && phoneSatePermission==true){
            token = homePresrenter.getBeanToken();
            System.out.println("token:"+token);
            homePresrenter.getTokenData(token);
        }

        Log.i("5", "onCreate: "+ Constant.getToken());
        
    }

    private void setComponet() {
        DaggerHomeFragmentComponet.builder()
                .homeFragmentModule(new HomeFragmentModule(this))
                .build()
                .in(this);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("欢迎界面"); //手动统计页面("SplashScreen"为页面名称，可自定义)
        MobclickAgent.onResume(this); //统计时长
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("欢迎界面"); //手动统计页面("SplashScreen"为页面名称，可自定义)，必须保证 onPageEnd 在 onPause 之前调用，因为SDK会在 onPause 中保存onPageEnd统计到的页面数据。
        MobclickAgent.onPause(this);
    }

    public void onStop() {

        super.onStop();
        finish();
    }


    private void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
                REQUEST_PERMISSION);
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 0:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    //用户授权
                    token = homePresrenter.getBeanToken();
                    System.out.println("token:"+token);
                    homePresrenter.getTokenData(token);
                } else {
                    //用户拒绝
                    Toast.makeText(this, "拒绝可能会导致一部分功能无法使用", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
